package com.kidback.bbloc259.ui.splash;

import android.view.View;

import com.kidback.bbloc259.app.KidApplication;
import com.kidback.bbloc259.app.provider.DeviceDataProvider;
import com.kidback.bbloc259.base.BaseActivity;
import com.kidback.bbloc259.base.lang.Delay;
import com.kidback.bbloc259.base.lang.Runnable0;
import com.kidback.bbloc259.bean.DeviceBean;
import com.kidback.bbloc259.databinding.SplashActivityBinding;
import com.kidback.bbloc259.ui.MainActivity;
import com.kidback.bbloc259.ui.WebViewActivity;
import com.kidback.bbloc259.util.ActivityLaunchUtils;
import com.kidback.bbloc259.util.PackageUtils;
import com.kidback.bbloc259.util.StatusBarUtils;
import com.kidback.bbloc259.util.TipUtils;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class SplashActivity extends BaseActivity<SplashActivityBinding> {

    private static final int SHOW_TIME = 3; //启动固定显示的时长，单位：秒

    private Disposable disposable;
    private long currentTime = SHOW_TIME;

    @Override
    public SplashActivityBinding getViewBinding() {
        return SplashActivityBinding.inflate(inflater);
    }

    @Override
    protected void initView() {
        StatusBarUtils.setStatusBarColor(activity, 0xff65b6cd);
        StatusBarUtils.changeStatusBarIconDark(activity);

        String str = String.format("跳过(%ds)", SHOW_TIME);
        viewBinding.tvTimeSecond.setText(str);

        viewBinding.tvTimeSecond.setOnClickListener(onClickListener);

        viewBinding.tvVersion.setText("当前版本：" + PackageUtils.getAppVersionName(activity));


        viewBinding.tvLinkPrivate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WebViewActivity.start(v.getContext(), "隐私政策", "https://www.kidback.top/BBLocationWeb/%E9%9A%90%E7%A7%81%E6%94%BF%E7%AD%96.html");
            }
        });

        viewBinding.tvLinkUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WebViewActivity.start(v.getContext(), "用户协议", "https://www.kidback.top/BBLocationWeb/%E7%94%A8%E6%88%B7%E5%8D%8F%E8%AE%AE.html");
            }
        });

        if (DeviceDataProvider.getInst().get() == null || !DeviceDataProvider.getInst().get().splash_privacy_show) {
            new PrivacyDialog(new PrivacyDialog.OnClickListener() {
                @Override
                public void onYes() {
                    startTimer();
                }

                @Override
                public void onNo() {

                    TipUtils.showMsgFixed(activity, "应用即将退出");
                    Delay.delay(1000, new Runnable0() {
                        @Override
                        public void run() {
                            android.os.Process.killProcess(android.os.Process.myPid());
                        }
                    });

                }
            }).show();

            if (DeviceDataProvider.getInst().get() == null) {
                DeviceDataProvider.getInst().put(new DeviceBean());
            }

            DeviceDataProvider.getInst().get().splash_privacy_show = true;
            DeviceDataProvider.getInst().save();
        } else {
            startTimer();
        }


    }

    private void startTimer(){
        disposable = Observable.interval(1000, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        currentTime--;

                        String str = String.format("跳过(%ds)", currentTime);
                        viewBinding.tvTimeSecond.setText(str);

                        if (currentTime <= 0) {
                            viewBinding.tvTimeSecond.setOnClickListener(null);
                            toNextPage();
                            disposable.dispose();
                        }
                    }
                });
    }

    @Override
    protected void loadData() {


    }


    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == viewBinding.tvTimeSecond) {
                if (disposable != null) {
                    disposable.dispose();
                }
                toNextPage();
            }

        }
    };

    private void toNextPage() {

        KidApplication.getInstance().init();

        ActivityLaunchUtils.launch(this, MainActivity.class);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (disposable != null) {
            disposable.dispose();
        }
    }

}
